package com.translator.engine;

import com.translator.config.AppConfig;
import com.translator.engine.ai.*;
import com.translator.engine.api.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 翻译引擎工厂类
 * 负责创建和管理不同类型的翻译引擎
 */
public class TranslationEngineFactory {
    
    private static final Map<String, Class<? extends TranslationEngine>> engineRegistry = new HashMap<>();
    
    // 注册所有可用的翻译引擎
    static {
        // AI翻译引擎
        engineRegistry.put("OpenAI", OpenAITranslationEngine.class);
        engineRegistry.put("Ollama", OllamaTranslationEngine.class);
        engineRegistry.put("Qwen", QwenTranslationEngine.class);
        engineRegistry.put("DeepSeek", DeepSeekTranslationEngine.class);
        engineRegistry.put("Claude", ClaudeTranslationEngine.class);
        
        // API翻译引擎
        engineRegistry.put("Baidu", BaiduTranslationEngine.class);
        engineRegistry.put("Google", GoogleTranslationEngine.class);
        engineRegistry.put("Microsoft", MicrosoftTranslationEngine.class);
    }
    
    /**
     * 获取所有可用的翻译引擎名称
     * 
     * @return 引擎名称列表
     */
    public static List<String> getAvailableEngineNames() {
        return new ArrayList<>(engineRegistry.keySet());
    }
    
    /**
     * 创建指定名称的翻译引擎实例
     * 
     * @param engineName 引擎名称
     * @return 翻译引擎实例，如果引擎不存在则返回null
     */
    public static TranslationEngine createEngine(String engineName) {
        if (engineName == null || !engineRegistry.containsKey(engineName)) {
            return null;
        }
        
        try {
            // 创建引擎实例
            TranslationEngine engine = engineRegistry.get(engineName).getDeclaredConstructor().newInstance();
            
            // 如果引擎需要API密钥，从配置中获取并设置
            if (engine.requiresApiKey()) {
                String apiKey = AppConfig.getInstance().getApiKey(engineName);
                engine.setApiKey(apiKey);
            }
            
            return engine;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}